package SingletonMode;

/**
 * @program: DesignModeProject
 * @description: 双重检查
 * @author: Li canzhang
 * @create: 2022-04-10 15:21
 **/
public class SingletonTest06 {
    public static void main(String[] args) {
        Singleton06 inst1 = Singleton06.getInst();
        Singleton06 inst2 = Singleton06.getInst();

        System.out.println(inst1.hashCode());
        System.out.println(inst2.hashCode());
    }
}

class Singleton06{
    //可以将修改的值立即更新到主存中
    private static volatile Singleton06 inst;
    private Singleton06(){};
    public static Singleton06 getInst(){
        //解决了懒加载问题
        if(inst == null){
            //解决了线程问题
            synchronized (Singleton06.class){
                if(inst == null){
                    inst = new Singleton06();
                }
            }
        }
        return inst;
    }
}